Make ArchFxVariableID hashable and usable in a mapping as a key#15
Merged
Make ArchFxVariableID hashable and usable in a mapping as a key#15
Conversation
By overriding the `__hash__()` method mainly. I've also overridden the `__eq__()` method to be sure we are consistent, considering that 2 ArchFXVariableID with the same slug are actually equal (instead of comparing the instance themselves).
Watkurem
approved these changes
Sep 18, 2025
Watkurem
left a comment
There was a problem hiding this comment.
It would make more sense to hash the integer value of the variable, but when in Rome...
Comment on lines
+44
to
+47
| def __eq__(self, other): | ||
| if isinstance(other, ArchFxCloudSlug): | ||
| return self._slug == other._slug | ||
| return super().__eq__(other) |
Contributor
Author
There was a problem hiding this comment.
Ahah yeah everything is a slug at the beginning, and so that's why hashing the slug directly covers all the cases at once...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview & Major Changes
As part of https://github.com/iotile/archfx_cloud/pull/4614, I needed to use an
ArchFxVariableIDas a key in a mapping. Yet it didn't work asArchFxVariableIDdoes not override the default__hash__()method and so each instance of it generates a different hash value.This PR fixes that by overriding the
__hash__()method to only consider the inner "slug" value.I've also overridden the
__eq__()method to be sure we are consistent, considering that 2 ArchFXVariableID with the same slug are actually equal (instead of comparing the instance themselves).